/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/11/26
 * Time: 06:18
 */
$(function(){
    var authorization = Cookies.get('_token');
    $.ajax({
        type: 'get',
        url: '/v1/manager/get/detail.json',
        data: {id: id},
        headers: {
            Authorization: authorization,
            Accept: "application/json; charset=utf-8"
        },
        success: function(e){
            var data = JSON.parse(e);
            if (data.status == 200) {
                var json = data.body.data;
                $('.title').text(json.title);
                $('.category').text(json.category_name);
                $('.desc').text(json.desc);
                if (json.pics.length > 0) {
                    var html = '';
                    $.each(json.pics, function(n, v){
                        html += '<a href="'+v+'" title="'+json.title+'" data-gallery=""><img src="'+v+'"></a>';
                    });
                    html += '<div id="blueimp-gallery" class="blueimp-gallery blueimp-gallery-controls" style="display: none;">';
                    html += '<div class="slides" style="width: 38160px;"></div>';
                    html += '<h3 class="title">图片</h3>';
                    html += '<a class="prev">&lt;</a>';
                    html += '<a class="next">&gt;</a>';
                    html += '<a class="close">×</a>';
                    html += '<a class="play-pause"></a>';
                    html += '<ol class="indicator"></ol>';
                    $('.work-pics').append(html);
                }
                if (json.local_score) {
                    $('.mark-score').addClass('hidden');
                    $('.score').text(json.local_score.score);
                    $('.btn-save').addClass('hidden');
                }
                if (json.score_list.length > 0) {
                    var scoreHtml = '';
                    $.each(json.score_list, function(n, v){
                        if (!json.local_score || json.local_score.teacher_id != v.teacher.id) {
                            scoreHtml += '<p><span>'+v.teacher.realname+':</span><span>'+v.score+'</span></p>';
                        }
                    });
                    $('.score-list').append(scoreHtml);
                    $('.total-teacher').val(json.score_list.length);
                } else {
                    $('.score-list').text('无');
                    $('.total-teacher').val(0);
                }
                $('.score-avg').text(json.score);
                $('.avg-score').val(json.score);
                $('.total-score').val(json.total_score);
            }
        }
    });

    $('.mark-score').on('keyup', function(){
        var local_score = parseFloat($(this).val());
        var total_score = parseFloat($('.total-score').val());
        var total_teacher = parseFloat($('.total-teacher').val());
        var avg_score = (total_score + local_score) / (total_teacher + 1);
        $('.avg-score').val(avg_score);
        $('.score-avg').text(avg_score);
    });

    $('.btn-save').on('click', function(){
        var params = {
            id: id,
            local_score: $('.mark-score').val(),
            score: $('.avg-score').val()
        };
        if (checkForm(params)) {
            $.ajax({
                type: 'post',
                url: '/v1/manager/saveScore',
                headers: {
                    Authorization: authorization,
                    Accept: "application/json; charset=utf-8"
                },
                data: {params: params},
                success: function(e){
                    var data = JSON.parse(e);
                    if (data.status == 200) {
                        alert('评分成功!');
                        location.href = '/manager/work.html';
                    }
                }
            });
        }
    });
});

function checkForm(params) {
    if (params.local_score == '' || params.local_score == 0) {
        alert('请输入评分!');
        return false;
    }
    return true;
}